<?php

	// +----------------------------------------------------------------------
	// | 行为日志控制器
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\controller\im\admin;

	use app\model\im\Robot as RobotModel;
	use app\controller\admin\Common;
    use app\model\store\StoreApp as StoreAppModel;
    use think\Exception;
	use think\facade\Env;
	use yicmf\builder\Edit;
	use yicmf\builder\Table;
	use app\model\cms\Document as DocumentModel;
    use app\model\ucenter\User as UserModel;
	use app\model\im\RobotGreetings as RobotGreetingsModel;
	use yicmf\builder\View;
	use think\facade\Url;

	class Robot extends Common
	{
		protected function initialize()
		{
			parent::initialize();
//            $this->event = new ChatGroupEvent();
		}

		public function _filter()
		{
			$map[] = ['status', '>', -2];
			$map[] = ['store_id', '=', $this->store['id']];

			return $map;
		}

		public function index()
		{

            $app  = StoreAppModel::where('store_id',$this->store['id'])->field('id,name as title')->select()->toArray();

            // 显示页面
			$builder = new Table();
			$builder->model(RobotModel::class)
				->where($this->_filter())
				->order('id DESC')
//				->suggest('支持当前页面参数的调用，例如：{$data.title}')
				->searchText('name', '场景')
                ->searchTabs('store_app_id',$app)
				->buttonUpdate()
				->keyId()
				->keyText('name', '场景')
//				->keyText('description', '描述')
//				->keyText('keyword', '关键词')
                //1关联知识库2欢迎语3客服无响应4顾客无响应5自动对话结束6客服手动结束7匹配关键词 11关键词二次任务

//				->keyMap('match_type', '匹配方式',RobotModel::$type_lists)
//				->keyText('delay', '触发延迟(秒)')
				->keyText('match_number', '响应次数')
//                ->keyText('sort', '排序')
//				->keyUpdateTime()
//				->keyStatus()
				->actionForbid()
				->actionRestore()
				->actionUpdate()
				->actionDelete();
			return $builder->fetch();
		}


		public function ai()
		{
            $map[] = ['type', '>=', 20];
            $map[] = ['status', '>', -2];
            $map[] = ['store_id', '=', $this->store['id']];

            // 显示页面
			$builder = new Table();
			$builder->model(RobotModel::class)
				->where($map)
				->order('id DESC')
//				->suggest('支持当前页面参数的调用，例如：{$data.title}')
				->searchText('reply', '回复内容')
				->buttonUpdate()
				->keyId()
				->keyText('name', '场景')
//				->keyText('description', '描述')
//				->keyText('keyword', '关键词')
                //1关联知识库2欢迎语3客服无响应4顾客无响应5自动对话结束6客服手动结束7匹配关键词20迎宾机器人

//				->keyMap('match_type', '匹配方式',RobotModel::$type_lists)
				->keyText('delay', '触发延迟(秒)')
				->keyText('match_number', '响应次数')
//                ->keyText('sort', '排序')
//				->keyUpdateTime()
//				->keyStatus()
				->actionForbid()
				->actionRestore()
				->actionUpdate('aiUpdate?id={$id}');
//				->actionDelete();
            return $builder->fetch('table_tree');
		}
		public function update()
		{
			if ($this->request->isPost()) {
				parent::_update();
			} else {
				$id = $this->request->get('id');
				// 判断是否为编辑模式
				$isEdit = $id ? true : false;
				if ($isEdit) {
					$data = RobotModel::where('id',$id)->where('store_id',$this->store['id'])->find();
				} else {
					$data = [];
					$data['store_id'] = $this->store['id'];
					$data['user_id'] = $this->user['id'];
				}
				$builder = new Edit();
				$builder->title($isEdit ? '编辑知识库' : '添加知识库')
                    ->keyHidden('store_id')
                    ->keyHidden('user_id')
//					->keyText('name', '名称')
					->keyText('keyword', '关键词')
                    ->keySwitch('is_common','共用定义')
                    ->keyRadio('content_type', '回复类型', ['自定义','常见问题','其他'])
                    ->keyBelongTo('document.title','im/admin/Document/index','关联常见问题')
					->keyEditor('reply', '回复内容')
                    ->keyNumber('delay','延迟回复','匹配到关键词后N秒后回复')
					->keySort()
					->keyRadio('match_type', '匹配方式',RobotModel::$type_lists)
					->data($data)
                    ->setTrigger('content_type',0,'reply')
                    ->setTrigger('content_type',1,'Document_id')
                    ->setTrigger('content_type',2,'')
					->buttonSubmit();
				return $builder->fetch();
			}
		}public function aiUpdate()
		{
			if ($this->request->isPost()) {
				parent::_update();
			} else {
				$id = $this->request->get('id');
				// 判断是否为编辑模式
				$isEdit = $id ? true : false;
				if ($isEdit) {
					$data = RobotModel::where('id',$id)->where('store_id',$this->store['id'])->find();
				} else {
					$data = [];
					$data['store_id'] = $this->store['id'];
					$data['user_id'] = $this->user['id'];
				}
				$builder = new Edit();
				$builder->title('编辑机器人')
                    ->keyHidden('store_id')
                    ->keyHidden('user_id')
//					->keyText('name', '名称')
//					->keyText('keyword', '关键词')
//                    ->keySwitch('is_common','共用定义')
//                    ->keyRadio('content_type', '回复类型', ['自定义','常见问题','其他'])
//                    ->keyBelongTo('document.title','im/admin/Document/index','关联常见问题')
					->keyEditor('reply', '回复内容')
                    ->keyNumber('delay','延迟回复','匹配到关键词后N秒后回复')
					->keySort()
//					->keyRadio('match_type', '匹配方式',RobotModel::$type_lists)
					->data($data)
//                    ->setTrigger('content_type',0,'reply')
//                    ->setTrigger('content_type',1,'Document_id')
//                    ->setTrigger('content_type',2,'')
					->buttonSubmit();
				return $builder->fetch();
			}
		}
        public function forbid()
        {
            $course = RobotModel::where('id', $this->request->param('id'))->find();
            if ($course) {
                $course['status'] = -1;
                if ($course->save()) {
                    $this->_returnSuccess('禁用成功');;
                } else {
                    $this->_returnError('禁用失败');
                }
            } else {
                $this->_returnError('操作异常');
            }
        }

        public function restore()
        {
            $course = RobotModel::where('id', $this->request->param('id'))->find();
            if ($course) {
                $course['status'] = 1;
                if ($course->save()) {
                    $this->_returnSuccess('启用成功');;
                } else {
                    $this->_returnError('启用失败');
                }
            } else {
                $this->_returnError('操作异常');
            }
        }
		public function greetings()
		{
			if ($this->request->isPost()) {
				$data = RobotGreetingsModel::where('user_id',$this->user['id'])->find();
				if ($data)
				{
					$data['type'] = $this->request->param('type');
					$data['content'] = $this->request->param('content');
					$data['store_id'] = $this->user['store_id'];
					$data->save();
					$this->_returnSuccess('设置已经更新', false);
				}else{
					$data['user_id'] = $this->user['id'];
					$data['store_id'] = $this->user['store_id'];
					$data['type'] = $this->request->param('type');
					$data['content'] = $this->request->param('content');
					RobotGreetingsModel::create($data);
					$this->_returnSuccess('设置已经更新', false);
				}
			} else {
				$data = RobotGreetingsModel::where('user_id',$this->user['id'])->find();
				if (!$data) {
					$data = [];
				}
				$builder = new Edit();
				$builder->title('问候语设置')
					->keyRadio('type', '方式', ['自定义','问好','无操作'],'',1)
					->keyEditor('content', '内容', '自定义内容','',['items'=>'simple'])
                    ->setTrigger('type',0,'content')
                    ->setTrigger('type',1,'')
                    ->setTrigger('type',2,'')
					->data($data)
					->buttonSubmit();
				return $builder->fetch();
			}
		}
		public function setting()
		{
			if ($this->request->isPost()) {
				$data = RobotGreetingsModel::where('user_id',$this->user['id'])->find();
				if ($data)
				{
					$data['type'] = $this->request->param('type');
					$data['content'] = $this->request->param('content');
					$data['store_id'] = $this->user['store_id'];
					$data->save();
					$this->_returnSuccess('设置已经更新', false);
				}else{
					$data['user_id'] = $this->user['id'];
					$data['store_id'] = $this->user['store_id'];
					$data['type'] = $this->request->param('type');
					$data['content'] = $this->request->param('content');
					RobotGreetingsModel::create($data);
					$this->_returnSuccess('设置已经更新', false);
				}
			} else {
                $data = UserModel::where('store_id',$this->user['store_id'])->where('type',0)->find();
				if (!$data) {
					$data = [];
				}
				$builder = new Edit();
				$builder->title('AI机器人设置')
					->keyBool('is_open', '开启')
                    ->keyTextInline('nickname','名称')
                    ->keyTextInline('nickname','名称')
					->keyEditor('content', '内容', '自定义内容','',['items'=>'simple'])
                    ->setTrigger('type',0,'content')
                    ->setTrigger('type',1,'')
                    ->setTrigger('type',2,'')
					->data($data)
					->buttonSubmit();
				return $builder->fetch();
			}
		}
	}
